<?php
class SpiderController extends ManagerController {
	public function SpiderController(){
		parent::ManagerController();
	}
	
	public function tags(){
		$basePath = ROOT_PATH . '/sites/main/' . MAIN_DOMAIN . '/file/spider';
		$tags = array();
		$handle = opendir($basePath);
		while (false !== ($file = readdir($handle))) {
			if($file == '.' || $file == '..') {
				continue;
			}
			$cur_path = $basePath .'/'. $file;
			if(is_dir($cur_path)) {
				$tags[] = iconv('gb2312', 'utf-8//IGNORE', $file);
			}
		}
		closedir($handle);
		$this->assign('tags', $tags);
		$this->display();
	}
	
	public function tag_images(){
		
		$this->id = urldecode($this->id);
		$base = '/sites/main/' . MAIN_DOMAIN . '/file/spider/' . $this->id;
		$basePath = ROOT_PATH . $base;
		if(is_dir($basePath)){
			echo 111;
		}else{
			echo $basePath;exit;
		}
		$tag_images = array();
		$handle = opendir($basePath);
		while (false !== ($file = readdir($handle))) {
			if($file == '.' || $file == '..') {
				continue;
			}
			$cur_path = $basePath .'/'. $file;
			echo $cur_path;
			if(is_file($cur_path)) {
				$img = iconv('gb2312', 'utf-8//IGNORE', $file);
				echo '1==>' . $img;
				$tag_images[] = $img;
			}
		}
		closedir($handle);
		$this->assign('images', $tag_images);
		$this->assign('base', $base);
		$this->display();
	}
	
	public function index(){
		if(IS_POST){
			$word = $_POST['word'];
			require_once ROOT_PATH . '/protected/includes/spider/wiki/BaiduSpider.php';
			if(isset($_POST['index'])){
				$index = intval($_POST['index']) + 1;
			}else{
				$index = null;
			}
			$data = BaiduSpider::analy($word, $index);
			if($data === false){
				
			}else{
				$this->assign($data);
			}
			if(isset($data['h2s'])){
				//词条选择
				$this->display('spider/word_select');
			}else{
				//词条分析
				$this->assign('cates', self::get_cate_options());
				$this->display('spider/analy');
			}
			exit;
		}
		$this->page_title = '百度词条爬虫 - ' . $this->site_info['name'];
		$this->display();
	}

	private function get_cate_options(){
		$mod = new DictCateModel();
		$dict_cates = $mod->get_by_key(SITE_ID);
		unset($dict_cates['_id']);
		$tree = new Tree($dict_cates);
		return $tree->get_options('　');
	}
	
	public function wiki(){
		if(IS_POST){
			$word = self::get_post_val('word');
			self::format($word);
			if(empty($word)){
				$this->error('词条不能为空！');
			}
			$mod = new DictModel();
			if($mod->exists_attrs(array('site_id'=>SITE_ID, 'word'=>$word))){
				$this->error('本词条已经存在！词条：' . $word);
			}
			$tag = self::get_post_val('tag');
			$tags = array();
			if(!empty($tag)){
				$tag_list = preg_split('/[\s|,|.|，|;]/', $tag);
				foreach ($tag_list as $t){
					$new_t = trim($t);
					if(!empty($new_t)){
						$tags[$new_t] = 1;
					}
				}
			}else{
				$tags = null;
			}
			$user_name = 'winter';
			$dict_cate = $_POST['dict_cate'];
			$time = time();
			$dict = array(
				'author' => $user_name,
				'contributors' => array(
					$user_name => 1,
				),
				'create_time' => $time,
				'creator' => $user_name,
				// 				'history' => array(),
				'site_id' => SITE_ID,
				'tags' => $tags,
				'update_time' => $time,
				'view_count' => 1,
				'edit_count' => 1,
				'word' => $word,
				'cate' => $dict_cate
			);
			$ps = $_POST['p'];
			//$ps = array_unique($ps);
			// 			$contents = $_POST['content'];
			$i = 1;
			$available_index = 0;
			$contents = $_POST['content'];
			foreach($ps as $new_p){
				self::format($new_p);
				if(empty($new_p)){
					++$i;
					continue;
				}
				$dict['items'][$new_p] = array(
						'author' => $user_name,
						'content' => $contents[$i-1],
						'rank' => $i,
						'time' => $time,
				);
				++$i;
				// 				++$available_index;
			}
			$mod->insert($dict);
			//用户记数添加
			$user_mod = new UserModel();
			$user_mod->inc($this->user_info['_id'], array(
				'wiki' => 1,
				// 	'wiki_index' => $available_index,
			));
			$this->redirect('/manager.php?app=spider');
		}
	}

	/**
	 * 格式化词条，将.换成全角的点号
	 * @param unknown_type $word
	 */
	private function format(&$word){
		$word = preg_replace('/\./', '．', $word);
		$word = trim($word);
		return $word;
	}
	
	public function w(){
		$this->display();
	}
	
	public function wiki_list(){
		$this->init_page_info(15);
		$mod = new Temp_wikiModel();
		$total = $mod->count();
		$this->set_page_total($total);
		if($total>0){
			$images = $mod->find(null, 'word,images,create_time', $this->page_info->get_start(), $this->page_info->page_size, array('create_time'=>1));
			foreach($images as $id => $item){
				if(!isset($item['images']) || count($item['images']) == 0){
					//如果没有图片
					$images[$id]['images_count'] = 0;
				}else{
					$images[$id]['images_count'] = count($item['images']);
				}
			}
			$this->assign('dicts', $images);
		}
		$this->page_title = '临时图片管理 - ' . $this->site_info['name'];
		$this->display();
	}
	
	public function image_list(){
		$mod = new Temp_wikiModel();
		$tw = $mod->get_by_key($this->id);

		$this->init_page_info(200);
		$this->set_page_total(count($tw['images']));
		$tw['images'] = array_slice($tw['images'], $this->page_info->get_start(), $this->page_info->page_size);
		foreach ($tw['images'] as $k=>&$src){
			$src = 'sites/main/' .MAIN_DOMAIN. '/file/spider' . preg_replace('/\\\\/', '/', $src);
		}
		
		$wiki_mod = new DictModel();
		$wiki = $wiki_mod->get_by_key_site_limit($this->id);
		$opts = array();
		if(!empty($wiki)){
			foreach ($wiki['items'] as $name=>$content){
				$opts[$name] = $name . ' (' . count($content['images']) . ')';
			}
			$this->assign('opts', $opts);
		}
		$this->assign('dict', $tw);
		$this->display();
	}
	
	public function del_dict(){
		$mod = new Temp_wikiModel();
		$tw = $mod->get_by_key($this->id);
		if(empty($tw)){
			$this->ajax_error('要删除的记录不存在或者已被删除！');
		}
		$mod->delete_by_key($this->id);
		$this->ajax_info('删除成功!');
	}
	
	public function del_image(){
		$images = $_POST['images'];
		$mod = new Temp_wikiModel();
		$tw = $mod->get_by_key($this->id);
		//获取图片路径，并删除图片文件
		foreach($images as $image_id){
			if(isset($tw['images'][$image_id])){
				$src = $tw['images'][$image_id];
				$src = ROOT_PATH . '/sites/main/' .MAIN_DOMAIN. '/file/spider' . preg_replace('/\\\\/', '/', $src);
				$this->_del_images ( $src );

			}
			$del_id = 'images.' . $image_id;
			$mod->delete_column_by_key($this->id, $del_id);
		}
		if(self::is_empty_wiki()){
			$this->redirect('/manager.php?app=spider&act=wiki_list');
		}else{
			$this->redirect('/manager.php?app=spider&act=image_list&id=' . $this->id);
		}
	}
	
	public function del_singal_image(){
		$image_id = $_REQUEST['image_id'];
		$mod = new Temp_wikiModel();
		$tw = $mod->get_by_key($this->id);
		//获取图片路径，并删除图片文件
		if(!isset($tw['images'][$image_id])){
			$this->ajax_error('您要删除的图片不存在或者已被删除！');
		}
		$src = $tw['images'][$image_id];
		$src = ROOT_PATH . '/sites/main/' .MAIN_DOMAIN. '/file/spider' . preg_replace('/\\\\/', '/', $src);
		$this->_del_images($src );
		$del_id = 'images.' . $image_id;
		$mod->delete_column_by_key($this->id, $del_id);
		self::is_empty_wiki();
		$this->ajax_info('删除成功！');
	}
	
	/**
	 * @param src
	 */private function _del_images($src) {
		if(is_file($src)){
			@unlink($src);
		}
		//删除规格图片
		$src = preg_replace('%^(.*?)/([^/]+)$%', '$1/300x300/$2', $src);
		if(is_file($src)){
			@unlink($src);
		}}

	
	public function image_set(){
		$images = $_POST['images'];
		$index = $_POST['index'];
		if(empty($index)){
			//没有设置目录
			$this->redirect('/manager.php?app=spider&act=image_list&id=' . $this->id);
			exit;
		}
		$mod = new Temp_wikiModel();
		$tw = $mod->get_by_key($this->id);
		
		$wiki_mod = new DictModel();
		$wiki = $wiki_mod->get_by_key_site_limit($this->id);
		$new_path = '/sites/main/' . MAIN_DOMAIN . '/file/' . date('Y_m', $wiki['create_time']) . '/' . $this->id;
		if(!is_dir(ROOT_PATH . $new_path)){
			yu_mkdirs(ROOT_PATH . $new_path);
		}
		$now = time();
		
		//获取图片路径
		$image_datas = array();
		foreach($images as $image_id){
			if(isset($tw['images'][$image_id])){
				$src = $tw['images'][$image_id];
				$src = ROOT_PATH . '/sites/main/' .MAIN_DOMAIN. '/file/spider' . preg_replace('/\\\\/', '/', $src);
				$image_name = $new_path . '/' . preg_replace('%^.*?/([^/]+)$%', '$1', $src);
				
				$image = array(
					'file'		=> $image_name,
					'author'	=> 'winter',
					'time'		=> $now,
					'count'		=> 1
				);
				
				//插入Dict
				$image_datas['items.' . $index . '.images.' . $image_id] = $image;
				
				//移动到新的位置
				rename($src, ROOT_PATH . $image_name);
				$this->_del_images($src);

				$del_id = 'images.' . $image_id;
				$mod->delete_column_by_key($this->id, $del_id);
				
				//设置images表
				$image['_id'] = $image_id;
				self::_set_image($wiki['word'], $index, $image);
			}
			$del_id = 'images.' . $image_id;
			$mod->delete_column_by_key($this->id, $del_id);
		}
		$image_datas['update_time'] = $now;
		$wiki_mod->update_by_key_site_limit($this->id, $image_datas);
		
		//清除WIKI缓存
		DictModel::clean_cache(MAIN_DOMAIN, $wiki['word']);
		
		if(self::is_empty_wiki()){
			$this->redirect('/manager.php?app=spider&act=wiki_list');
		}else{
			$this->redirect('/manager.php?app=spider&act=image_list&id=' . $this->id);
		}
	}

	
	private function is_empty_wiki(){
		$mod = new Temp_wikiModel();
		$tw = $mod->get_by_key($this->id);
		if(empty($tw['images'])){
			$mod->delete_by_key($this->id);
			//删除整个目录
			yu_del_dir(ROOT_PATH . '/sites/main/' . MAIN_DOMAIN . '/file/spider/' . $this->id);
			return true;
		}
		return false;
	}
	
	public function repair_image(){
		$dict_mod = new DictModel();
		$this->init_page_info(50);
		$total = $dict_mod->count();
		$this->set_page_total($total);
		
		$dicts = $dict_mod->find(null, null, $this->page_info->get_start(), $this->page_info->page_size);
		if(count($dicts)==0){
			echo 'Finish!';
			exit;
		}	
		foreach($dicts as $dict){
			foreach($dict['items'] as $index=>$item){
				if(!isset($item['images']) || count($item['images']) === 0)
					continue;
				if(isset($item['images'][0])){
					//如果是数字，则转换成UUID
					$old_images = $item['images'];
					$new_images = array();
					foreach($old_images as $image){
						$new_images[gen_guid()] = $image;
					}
					$item['images'] = $new_images;
					//更新
					$dict_mod->update_by_key($dict['_id'], array('items.' . $index . '.images'=>$new_images));
				}
				foreach($item['images'] as $image_id=>$image){
					//处理图片
					$image['_id'] = $image_id;
					self::_set_image($dict['word'], $index, $image);
				}
			}
		}
		//下一页
		$page = $this->page_info->page + 1;
		if($page > $this->page_info->page_count){
			echo 'Finish!';
			exit;
		}
		$next_page = '/manager.php?app=spider&act=repair_image&page=' . $page;
		echo '
		Next Page: ' . $page . '
		<script>
			window.location.href="' . $next_page . '";
		</script>
		';
	}
	
	private function _set_image($word, $index, $image){
		$mod = new ImageModel();
		if($mod->exists_key($image['_id'])){
			return;
		}
		//获取图片地址
		$file = $image['file'];
		if(!is_file(ROOT_PATH . $file)){
			return;
		}
		$image_attr = getimagesize(ROOT_PATH . $file);
		$size = filesize(ROOT_PATH . $file);
		$img = array(
			'_id' 			=>  $image['_id'],
			'file' 			=>  $file,
			'width' 		=>  $image_attr[0],
			'height' 		=>  $image_attr[1],
			'size' 			=>  $size,
			'author' 		=>  $image['author'],
			'collect' 		=>  0,
			'create_time' 	=>  $image['time'],
			'desc' 			=>  '',
			'title' 		=>  $word . '-' . $index,
			'from' 			=>  '/wiki/' . $word . '#' . $index,
			'hate' 			=>  0,
			'like' 			=>  0,
			'site_id' 		=>  SITE_ID
		);
		$mod->insert($img);
	}
	
	public function scan(){

		$dict_mod = new DictModel();
		$this->init_page_info(50);
		$total = $dict_mod->count();
		$this->set_page_total($total);
		
		$dicts = $dict_mod->find(null, null, $this->page_info->get_start(), $this->page_info->page_size);
		if(count($dicts)==0){
			echo 'Finish!';
			exit;
		}

		foreach($dicts as $dict){
			$total_image = 0;
			foreach ($dict['items'] as $index=>$item){
				if(isset($item['images'])){
					$total_image += count($item['images']);
				}
			}
			if($total_image < 20){
				//如果小于20，则需要重新下载
				
// 				file_get_contents('http://solr.localhost:8083/w/spider.add?name=' . $dict['word']);
				file_get_contents('http://solr.duoec.com:8080/w/spider.add?name=' . urlencode($dict['word']));
				echo $dict['word'] . '<br>';
			}
		}
		//下一页
		$page = $this->page_info->page + 1;
		if($page > $this->page_info->page_count){
			echo 'Finish!';
			exit;
		}
		$next_page = '/manager.php?app=spider&act=scan&page=' . $page;
		echo '
		Next Page: ' . $page . '
		<script>
		setTimeout(function(){
		window.location.href="' . $next_page . '";
		}, 1000);
		</script>
		';
	}
}

?>